package com.bianguo.android.bprinter.view.activity;

import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.baselibrary.view.BaseActivity;
import com.bianguo.android.bprinter.R;
import com.bianguo.android.bprinter.view.widget.ZoomView2;
import com.bianguo.android.bprinter.view.widget.ZoomViewLayout2;
import com.bianguo.android.ioclibrary.annotations.BindView;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class PaperActivity extends BaseActivity implements ZoomViewLayout2.OnClickCutPoint {

    @BindView(R.id.paper_zoom_layout)
    ZoomViewLayout2 zoomLayout;
    @BindView(R.id.paper_text)
    TextView paperTv;

    @Override
    protected int layoutId() {
        return R.layout.activity_paper;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

        zoomLayout.addZoomView(BitmapFactory.decodeResource(getResources(), R.drawable.test_cat), true);
        zoomLayout.addZoomView(BitmapFactory.decodeResource(getResources(), R.drawable.test_cat), true);

        String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(absolutePath, "aaa.jpg");
        if (file.exists()) {
            zoomLayout.addZoomView(BitmapFactory.decodeFile(file.getAbsolutePath()), true);
        }
        zoomLayout.setOnClickCutPoint(this);
//        readAssetsFile();
    }

    private void readAssetsFile() {
        AssetManager assets = getResources().getAssets();
        new Thread(() -> {
            try {
                long startTime = System.currentTimeMillis();
                InputStream is = assets.open("test2.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                long endTime = System.currentTimeMillis();
                runOnUiThread(() -> {
                    paperTv.setText(sb.toString());
                });
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    @Override
    public void onClickCutPoint(ZoomView2 zoomView) {
//        zoomLayout.updateBitmap(zoomView, BitmapFactory.decodeResource(getResources(), R.drawable.test_code));
        zoomLayout.updateBitmap(zoomView, BitmapFactory.decodeResource(getResources(), R.drawable.d_haixiu));
    }
}
